package com.netdisk.service.impl;

import com.netdisk.dto.UserActivityDTO;
import com.netdisk.dto.request.file.CreateShareDTO;
import com.netdisk.dto.response.file.FileDTO;
import com.netdisk.dto.response.file.ShareDTO;
import com.netdisk.dto.response.file.ShareListDTO;
import com.netdisk.entity.File;
import com.netdisk.entity.Share;
import com.netdisk.exception.BusinessException;
import com.netdisk.mapper.FileMapper;
import com.netdisk.mapper.ShareMapper;
import com.netdisk.mapper.UserMapper;
import com.netdisk.service.ShareService;
import com.netdisk.service.UserActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ShareServiceImpl implements ShareService {

    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private ShareMapper shareMapper;
    @Autowired
    private UserActivityService userActivityService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 创建分享
     * @param createShareDTO
     * @return
     */
    @Override
    public ShareDTO createShare(CreateShareDTO createShareDTO) {
        ShareDTO shareDTO = new ShareDTO();
        Integer fileId = createShareDTO.getFileId();
        Integer userId = createShareDTO.getUserId();
        File file = fileMapper.getFileById(userId,fileId);
        if(file != null && file.getDeletedAt() == null) {
            Share share = new Share();
            FileDTO fileDTO = toFileDTO(file);
            /*生成shareId*/
            String shareId = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
            share.setShareId(shareId);
            share.setFileId(fileId);
            share.setUserId(userId);
            shareMapper.insert(share);

            Share savedShare = shareMapper.findByShareId(shareId);
            BeanUtils.copyProperties(savedShare,shareDTO);
            shareDTO.setFile(fileDTO);
            userActivityService.insertActivity(file, "分享文件", "share");

            return shareDTO;
        }
        else throw new BusinessException("文件不存在");
    }

    /**
     * 获取分享信息
     * @param shareId
     * @return
     */
    @Override
    public ShareDTO getShareInfoByShareId(String shareId) {
        Share share = shareMapper.findByShareId(shareId);

        if (share != null){
            Integer userId = share.getUserId();
            Integer fileId = share.getFileId();
            File file = fileMapper.getFileById(userId,fileId);
            ShareDTO shareDTO = new ShareDTO();
            BeanUtils.copyProperties(share,shareDTO);
            FileDTO fileDTO = toFileDTO(file);
            shareDTO.setFile(fileDTO);
            return shareDTO;
        }
        else throw new BusinessException("分享不存在");
    }

    /**
     * File转为FileDTO
     * @param file
     * @return
     */
    public FileDTO toFileDTO(File file) {
        FileDTO fileDTO = new FileDTO();
        BeanUtils.copyProperties(file,fileDTO);
        return fileDTO;
    }

    /**
     * 获取用户分享数量
     * @param userId
     * @return
     */
    public Integer getShareCountById(Integer userId){
        return shareMapper.getShareCountById(userId);
    }

    /**
     * 根据用户Id获取分享列表
     * @param userId
     * @return
     */
    public List<ShareDTO> getShareListById(Integer userId){
        List<Share> shareList = shareMapper.getShareListById(userId);
        List<ShareDTO> shareDTOList = new ArrayList<>();
        for (Share s : shareList) {
            if (s != null && s.getDeletedAt() == null) {
                Integer fileId = s.getFileId();
                File file = fileMapper.getFileById(userId, fileId);
                ShareDTO shareDTO = new ShareDTO();
                shareDTO.setFile(toFileDTO(file));
                BeanUtils.copyProperties(s, shareDTO);
                shareDTOList.add(shareDTO);
            }
        }
        return shareDTOList;
    }

    /**
     * 取消分享
     * @param shareId
     * @param userId
     */
    public void cancelShare(String shareId, Integer userId){
        Share share = shareMapper.findByShareId(shareId);
        if (share != null){
            if (share.getUserId().equals(userId)) shareMapper.delete(share);
            else throw new BusinessException("无权限取消分享");
        }
        else throw new BusinessException("分享不存在");
    }
}
